/*
Automatic clinic tests using a robot arm.
Copyright (C) 2010  CIC-CPC, CHU Gabriel Montpied
All other trademarks, logos and copyrights are the property
of their respective owners.

This file is part of cpc-painbot.

cpc-painbot is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

cpc-painbot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with cpc-painbot.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "widgetindicateur.h"
#include "ui_widgetindicateur.h"

#include "qserialdevicewatcher/serialdevicewatcher.h"

#include "indicateurprocess.h"
#include "capteur.h"

#include <QDebug>

WidgetIndicateur::WidgetIndicateur(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetIndicateur),
    indicateur_(new IndicateurProcess(this)),
    capteur_(new Capteur(this))
{
    indicateur()->setCapteur(capteur());
    ui->setupUi(this);

    genererListePorts();

    // Signaux émis depuis l'indicateur.
    connect(indicateur(), SIGNAL(minimumChange(qreal)), this, SLOT(actualiserMinimum(qreal)));
    connect(indicateur(), SIGNAL(maximumChange(qreal)), this, SLOT(actualiserMaximum(qreal)));
    connect(indicateur(), SIGNAL(tareChangee(qreal)), this, SLOT(actualiserTare(qreal)));
    connect(indicateur(), SIGNAL(adresseChangee(int)), ui->adresseSpinBox, SLOT(setValue(int)));

    // Signaux émis depuis le capteur.
    connect(capteur_, SIGNAL(mesure(QVariant)), this, SLOT(actualiserMesure(QVariant)));

    // Signaux à destination de l'indicateur.
    connect(ui->connecterPushButton, SIGNAL(clicked(bool)), indicateur_, SLOT(connecter(bool)));
    connect(ui->portSerieComboBox, SIGNAL(currentIndexChanged(QString)), indicateur_, SLOT(setPort(QString)));
    connect(ui->mesurePushButton, SIGNAL(clicked()), indicateur_, SLOT(mesure()));
    connect(ui->majMinPushButton, SIGNAL(clicked()), indicateur_, SLOT(majMin()));
    connect(ui->majMaxPushButton, SIGNAL(clicked()), indicateur_, SLOT(majMax()));
    connect(ui->razMinPushButton, SIGNAL(clicked()), indicateur_, SLOT(resetMin()));
    connect(ui->razMaxPushButton, SIGNAL(clicked()), indicateur_, SLOT(resetMax()));
    connect(ui->razTarePushButton, SIGNAL(clicked()), indicateur_, SLOT(resetTare()));
    connect(ui->prendreTarePushButton, SIGNAL(clicked()), indicateur_, SLOT(setTare()));
    connect(ui->adresseSpinBox, SIGNAL(valueChanged(int)), indicateur_, SLOT(setAdresse(int)));
}

WidgetIndicateur::~WidgetIndicateur()
{
    delete ui;
}

void WidgetIndicateur::genererListePorts()
{
    SerialDeviceWatcher *ports = new SerialDeviceWatcher(this);

    ui->portSerieComboBox->clear();

    foreach (QString port, ports->devicesAvailable())
        ui->portSerieComboBox->addItem(port);
}

void WidgetIndicateur::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void WidgetIndicateur::actualiserMesure(const QVariant &mesure)
{
    qDebug() << "Actualisation de la mesure :" << mesure;
    ui->mesureLabel->setNum(mesure.toDouble());
}

void WidgetIndicateur::actualiserMinimum(qreal min)
{
    qDebug() << "Actualisation du min :" << min;
    ui->valeurMinLabel->setText(tr("Min = %1").arg(min));
}

void WidgetIndicateur::actualiserMaximum(qreal max)
{
    qDebug() << "Actualisation du max :" << max;
    ui->valeurMaxLabel->setText(tr("Max = %1").arg(max));
}

void WidgetIndicateur::actualiserTare(qreal tare)
{
    qDebug() << "Actualisation de la tare :" << tare;
    ui->tareLabel->setText(tr("Tare = %1").arg(tare));
}
